layui.define(['form', 'table', 'element'], function(exports){
  var $ = layui.$,table = layui.table,form = layui.form, element = layui.element;

  var memberId =$('input[name="id"]').val();

    table.render({
        elem: '#LAY-table-address'
        ,url: layui.setter.webRoot + '/member/address/list.data'
        ,where:{memberId:memberId}
        ,method: 'post'
        ,parseData: function (res) {
            return {
                "code": res.code,
                "msg": res.msg,
                "count": res.body.total,
                "data": res.body.data
            };
        }
        ,response: {statusCode: 200}
        ,cols: [[
            {field: 'id', width: 100,title: 'ID'}
            ,{field: 'receiveName', title: '收件人'}
            ,{field: 'receivePhone', title: '联系电话'}
            ,{field: 'province', title: '省份'}
            ,{field: 'city', title: '城市'}
            ,{field: 'address', title: '详细地址'}
            ,{field: 'updateDate',  title: '最后修改时间'}
            ,{title: '操作', width: 80, align:'center', fixed: 'right', toolbar: '#LAY-table-address-toolbar'}
        ]]
        ,page: true
        ,height: 'full'
        ,text: '对不起，加载出现异常！'
    });

    element.on('tab(tab-info)', function (data) {
        if(data.index== 0){
            table.reload('LAY-table-address')
        }
    });

    var active = {
        delAddress:function (obj){
            layer.confirm('确定要删除该快递地址吗？', function(index){
                $.ajax({
                    type: "post",
                    url: layui.setter.webRoot + '/member/address/del.do',
                    data:{memberId:obj.data.memberId,addressId:obj.data.id},
                    dataType:"json",
                    success:function(res){
                        if(res.code == 200){
                            obj.del();
                        }else{
                            layer.msg(res.msg,{icon: 5});
                        }
                    }
                });
                layer.close(index);
            });
        }
    }

    $('.layui-btn.LAY-btn-fun').on('click', function(){
        var type = $(this).data('type');
        active[type] ? active[type].call(this) : '';
    });

    table.on('tool(LAY-table-address)', function(obj){
        var type = obj.event ;
        active[type] ? active[type].call(this,obj) : '';
    });
  
  exports('member_info', {})
});